﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Collections.TreeCollections;
using System.Xml.Linq;
using System.IO;
using Collections;

namespace FeatureModelTool.ProjectView.ProjectTreeStructures
{
    public class ProjectFolder : TreeNode
    {
        #region XML Sybol
        private readonly static string _XML_FOLDER_NAME = "FolderName";
        #endregion

        #region Properties
        public override string Name { get { return this._folderName; } }
        public string FolderName
        {
            get { return this._folderName; }
            set
            {
                this.TreeOwner.PreProcess(this, null);
                _folderName = value;
                this.TreeOwner.ProProcess(this, null);
            }
        }
        public string FullPath { get { return getFullFilePath(); } }
        #endregion

        #region Fields
        private string _folderName;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="folderName"></param>
        public ProjectFolder(TreeNodeIdentify id, string folderName, bool create): base(id)
        {
            _folderName = folderName;
        }

        /// <summary>
        /// Constain File
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool ContainFile(string fileName)
        {
            foreach (INonRoot nonRoot in this.Children)
            {
                ProjectFile file = nonRoot as ProjectFile;
                if (file == null)
                {
                    continue;
                }
                if (fileName == file.Name)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// args[0]: string folderName
        /// </summary>
        /// <param name="id"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static TreeObject ConstructFolder(TreeNodeIdentify id, object[] args)
        {
            string folderName = string.Empty;
            bool create = false;
            if (args != null)
            {
                folderName = args[0].ToString();
                create = (bool)args[1];
            }
            return new ProjectFolder(id, folderName, create);
        }

        #region XML Parser
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override XElement ToXElem()
        {
            XElement xelem = base.ToXElem();
            xelem.Add(new XAttribute(_XML_FOLDER_NAME, _folderName));
            return xelem;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xelem"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        internal static void xmlParser(XElement xelem, TreeObject obj)
        {
            ProjectFolder folder = obj as ProjectFolder;
            if (folder == null)
            {
                return;
            }
            folder._folderName = xelem.Attribute(_XML_FOLDER_NAME).Value;
        }
        #endregion


        /// <summary>
        /// Get full file path
        /// </summary>
        /// <returns></returns>
        private string getFullFilePath()
        {
            string parentPath = string.Empty;
            if (this.Parent is ProjectFolder)
            {
                ProjectFolder folder = (ProjectFolder)this.Parent;
                parentPath = folder.FullPath;
            }
            else if (this.Parent is ProjectTreeRoot)
            {
                ProjectTreeRoot root = (ProjectTreeRoot)this.Parent;
                parentPath = root.FullPath;
            }
            else
            {
                return null;
            }

            string fullFilePath = parentPath + _folderName + "\\";
            return fullFilePath;
        }

        public override TreeRoot Parse2TreeRoot()
        {
            throw new NotImplementedException();
        }

        public override object Clone()
        {
            throw new NotImplementedException();
        }
    }
}
